package com.delaphone.flocash.web.action;

import com.delaphone.flocash.web.bean.UserProfile;
import com.delaphone.flocash.model.FUser;
import com.delaphone.flocash.service.UserService;
import com.delaphone.flocash.util.SystemConts;
import com.delaphone.flocash.util.StringUtil;
import com.delaphone.flocash.web.util.Validate;
import com.opensymphony.xwork2.Action;

/**
 * Login Action Controller
 * @author tamdx, dongtv
 */
public class LoginAction extends BaseAction {

    private String username;
    private String password;
    private String captcha;
    private UserService userService;

    public String index() throws Exception {
        if (user != null) { //if user is logged
            return Action.SUCCESS;
        }
        return Action.LOGIN;
    }

    public String check() throws Exception {
        //if user is logged ==> forward to summary
        if (user != null && user.isLogged()) {
            return Action.SUCCESS;
        }
        if (Validate.isEmptyString(username)) {
            addFieldError("username", "*");
        } else if (!Validate.isEmail(username)) {
            //addActionError(getText("login.username.invalid"));
        }
        if (Validate.isEmptyString(password)) {
            addFieldError("password", "*");
        }
        if (Validate.isEmptyString(captcha)) {
            addFieldError("captcha", "*");            
        }
        if(!getFieldErrors().isEmpty()){
            addActionError(getText("login.fields.missing"));
        }
        if (!getActionErrors().isEmpty()) {
            return Action.INPUT;
        }
        String c = (String) session.getAttribute("Captcha");
        if (c == null || !captcha.toLowerCase().equals(c.toLowerCase())) {
            addActionError(getText("login.captcha.error"));
            return Action.INPUT;
        }

        FUser u = userService.getUserLogin(username, password);
        if (u == null) {//wrong email or password
            addActionError(getText("login.failed.warning"));
            logger.warn("Logged in failed, user: " + username);
            return Action.INPUT;
        } else {//Logged in successfull --> create user object in session
            logger.info("Logged in successful, user: " + username);
            user = new UserProfile();
            user.setId(u.getId());
            user.setEmail(username);
            user.setLogged(true);
            user.setLastlogTime(StringUtil.formatDisplayDateTime(u.getLastLogin()));
            user.setAccountType(u.getAccountType());
            user.setPhoneNumber(u.getPhoneNumber());
            user.setActive(u.getActive());
            user.setAccount(userService.getAccountByEmail(username, SystemConts.DEFAULT_CURRENCY));
            user.setTxLimit(u.getTxLimit());
            user.setDailyLimit(u.getDailyLimit());
            user.setDepositLimit(u.getDepositLimit());
            user.setCurrency(SystemConts.DEFAULT_CURRENCY);
            session.setAttribute("user", user);
            userService.saveDateLogin(username);
            //get account number

            return Action.SUCCESS;
        }
    }
    // <editor-fold defaultstate="collapsed" desc="get and set methods">

    public String getCaptcha() {
        return captcha;
    }

    public void setCaptcha(String captcha) {
        this.captcha = captcha;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public UserService getUserService() {
        return userService;
    }

    public void setUserService(UserService userService) {
        this.userService = userService;
    }
    // </editor-fold>
}
